package top.jnaw.test;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.Const;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.IDataSourceProvider;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import org.apache.log4j.Logger;
import lzj.tools.SqlUtil;
import top.jnaw.test.model._MappingKit;
import top.jnaw.test.routes.MainRoutes;

//import org.slf4j.Logger;


/**
 * Created by neo on 17-10-12.
 */
public class MainConfig extends JFinalConfig {

  private Routes globalRoutes;
  private Logger log = Logger.getLogger(MainConfig.class);

  @Override
  public void configConstant(Constants me) {
    // [Neo] init config file and log

    PropKit.use("product.properties");
//    PropKit.use("developer.properties");

    log.info(PropKit.get("project_name"));

//    me.setMaxPostSize(1024 * 1024 * 150);//请求体大小设置\
    me.setMaxPostSize(200* Const.DEFAULT_MAX_POST_SIZE);
    //true开发者模式，false生产环境性能好
    me.setDevMode(PropKit.getBoolean("dev_mode",false));
  }

  @Override
  public void configRoute(Routes me) {
    // [Neo] add more routes
    me.add(new MainRoutes());

    // [Neo] copy into global,  shiro需要使用
    globalRoutes = me;
  }

  @Override
  public void configEngine(Engine me) {

  }

  @Override
  public void configPlugin(Plugins me) {
    //配置数据库连接池
    IDataSourceProvider provider = SqlUtil.dataSourcePlugin();
    ActiveRecordPlugin arp = new ActiveRecordPlugin(provider);
//    me.add(arp);
    arp.setShowSql(PropKit.getBoolean("show_sql"));
    _MappingKit.mapping(arp);
    me.add(arp);


    //TODO 这是多个数据库的配置方法
//    IDataSourceProvider provider2 = DbUtil.FuXindataSourcePlugin();
//    ActiveRecordPlugin arp2 = new ActiveRecordPlugin("fuxin",provider2);
////    arp2.setDialect(new AnsiSqlDialect());
//    me.add(arp2);


    // TODO 开启用户权限验证并配置跳转路径
    ShiroPlugin shiroPlugin = new ShiroPlugin(globalRoutes);
    shiroPlugin.setLoginUrl("/LoginUrl");//登陆url：未验证成功跳转
//    shiroPlugin.setSuccessUrl("/index.do");//登陆成功url：验证成功自动跳转
    shiroPlugin.setUnauthorizedUrl("/UnauthorizedUrl");//授权url：未授权成功自动跳转
    me.add(shiroPlugin);
    //开启缓存机制
    me.add(new EhCachePlugin());

  }
  //全局拦截器
  @Override
  public void configInterceptor(Interceptors me) {
    //开启Shiro全局拦截器
    me.add(new ShiroInterceptor());
  }

  @Override
  public void configHandler(Handlers me) {
  }
}
